﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace LILI.Data
{
    public class DataHelper
    {
        #region 执行SQL语句，无返回值
        /// <summary>
        /// 执行SQL语句，无返回值
        /// </summary>
        /// <param name="strSql"></param>
        public static int ExecuteSql(string strSql,SqlParameter[] parameter)
        {
            try
            {
                return SqlHelper.ExecuteNonQuery(SqlHelper.connectionString, CommandType.Text, strSql,parameter);
            }
            catch (SqlException e)
            {
                return 0;
            }
        }
        #endregion
        #region  增加的一个函数
        /// <summary>
        /// 运行SQl语句返回第一条记录的第一个字段的值。区分内部函数调用的情况
        /// </summary>
        /// <param name="text">SQL语句，或者参数化的SQL语句</param>
        /// <returns></returns>
        public static T ExecuteScalar<T>(string strSql)
        {
            try
            {
                T re = (T)Convert.ChangeType(SqlHelper.ExecuteScalar(SqlHelper.connectionString, CommandType.Text, strSql), typeof(T));
                return re;
            }
            catch (Exception ex)
            {
                return default(T);
            }
        }
        /// <summary>
        /// 运行SQl语句返回第一条记录的第一个字段的值。区分内部函数调用的情况
        /// </summary>
        /// <param name="text">SQL语句，或者参数化的SQL语句</param>
        /// <returns></returns>
        public static T ExecuteScalar<T>(string strSql,SqlParameter[] parameters)
        {
            try
            {
                T re = (T)Convert.ChangeType(SqlHelper.ExecuteScalar(SqlHelper.connectionString, CommandType.Text, strSql,parameters), typeof(T));
                return re;
            }
            catch (Exception ex)
            {
                return default(T);
            }
        }
        #endregion

        #region 执行SQL返回Table
        /// <summary>
        /// 执行SQL返回Table
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public static DataTable GetDataTable(string sql)
        {
            try
            {
                return SqlHelper.ExecuteDataset(SqlHelper.connectionString, CommandType.Text, sql).Tables[0];
            }
            catch (SqlException e)
            {
                return null;
            }
        }
        #endregion

        #region 执行查询语句，返回SqlDataReader
        /// <summary>
        /// 执行查询语句，返回SqlDataReader ( 注意：调用该方法后，一定要对SqlDataReader进行Close )
        /// </summary>
        /// <param name="strSQL">查询语句</param>
        /// <returns>SqlDataReader</returns>
        public static SqlDataReader ExecuteReader(string strSQL,SqlParameter[] parameters)
        {
            return SqlHelper.ExecuteReader(SqlHelper.connectionString, CommandType.Text, strSQL,parameters);
        }
        #endregion

        #region 执行多条SQL语句，实现数据库事务
        /// <summary>
        /// 执行多条SQL语句，实现数据库事务。
        /// </summary>
        /// <param name="SQLStringList">多条SQL语句</param>	
        public static int ExecuteSqlTran(List<String> SQLStringList)
        {
            return SqlHelper.ExecuteSqlTran(SQLStringList);
        }
        #endregion

        
    }
}
